-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.py
More file actions
45 lines (40 loc) · 899 Bytes
/
Solution.py
File metadata and controls
45 lines (40 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
MAX = 100
stack = []
top = -1
def push():
global top
if top == MAX - 1:
print("Stack overflow!")
return
val = int(input("Enter value to push: "))
stack.append(val)
top += 1
print(f"{val} pushed onto stack.")
def pop():
global top
if top == -1:
print("Stack underflow!")
return
val = stack.pop()
top -= 1
print(f"{val} popped from stack.")
def display():
if not stack:
print("Stack is empty.")
return
print("Stack elements:")
for i in range(len(stack) - 1, -1, -1):
print(stack[i])
while True:
print("\n1. Push\n2. Pop\n3. Display\n4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
push()
elif choice == 2:
pop()
elif choice == 3:
display()
elif choice == 4:
break
else:
print("Invalid choice!")